//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics.Contracts;
using System.Text;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    public class RichTextParagraph: IOfficeXmlSerializableAs
    {
        public RichTextParagraph()
        {
            _runs.CollectionChanged += runs_CollectionChanged;
        }
#if DEBUG
        public override string ToString()
        {
            //\\Contract.Assert(false);
            return base.ToString();
        }
#endif


        public RichTextParagraph(RichTextRun run): this()
        {
            Runs.Add(run);
        }
        public RichTextParagraph(string text): this(new RichTextRun(text))
        {
        }

        private Cell _cell;

        public Cell Cell
        {
            get { return _cell; }
            set { 
                _cell = value;
                if (_cell == null)
                    return;
                foreach (RichTextRun run in Runs)
                    run.OnAttach(value);
            }
        }
	
        void runs_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Reset:
                    foreach (RichTextRun run in Runs)
                        run.Paragraph = this;
                    break;
                case NotifyCollectionChangedAction.Remove:
                    foreach (RichTextRun run in e.OldItems)
                        run.Paragraph = null;
                    break;
                case NotifyCollectionChangedAction.Add:
                    foreach (RichTextRun run in e.NewItems)
                        run.Paragraph = this;
                    break;
                case NotifyCollectionChangedAction.Replace:
                    foreach (RichTextRun run in e.NewItems)
                        run.Paragraph = this;
                    foreach (RichTextRun run in e.OldItems)
                        run.Paragraph = null;
                    break;
                case NotifyCollectionChangedAction.Move:
                    break;
                default: throw new NotSupportedException();
            }
        }
        private readonly ObservableCollection<RichTextRun> _runs = new ObservableCollection<RichTextRun>();
        public ObservableCollection<RichTextRun> Runs
        {
            get { return _runs;
            }
        }
        internal string Text
        {
            get
            {
                var sb = new StringBuilder();
                if (_text != null)
                    sb.Append(_text);
                foreach (RichTextRun r in Runs)
                    sb.Append(r.Text);
                return sb.ToString();
            }
        }
        string _text;
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializableAs.ReadXmlAs (string name, ReadContext c)
        {
            using (ReadContext c2 = c.Read(name))
            {
                //\\string s = "";
                if (c2.ReaderLocalName == "t")
                {
                    _text = c2.ReadElementString("t");
                }
                else
                {
                    while (c2.ReaderLocalName == "r")
                    {
                        Runs.Add(c2.ReadElement<RichTextRun>());
                    }

                }
                while (c2.ReaderLocalName == "rPh") //\\
                    c2.EatForNow("rPh");
                c2.EatForNow("phoneticPr");
            }
        }

        void IOfficeXmlSerializableAs.WriteXmlAs(string name, WriteContext context)
        {
            using (context.Write(name))
            {
                foreach (RichTextRun r in Runs)
                    context.WriteElement(r);
            }
        }

        #endregion
    }
}
